# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
from pymongo import MongoClient


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # 'User-Agent': ua.random,
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'ASP.NET_SessionId=no4k2e30trxpidla2y0ryhsb; loginName=MyExam=7B7514E9FDA7171D0F0085F2794DD026; loginPwd=MyExam=707EAD79E9F21DC1',
            'DNT': '1',
            'Host': 'qqks.net',
            'Origin': 'http://qqks.net',
            'Referer': 'http://qqks.net/Exercises-8-1764.html?IsKeyNote=false',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'

        }

    # 练习-目录
    def get_mulu(self, kemu):
        url = "http://qqks.net/Chapter-9-1.html?IsKeyNote=false"
        res = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(res.text)
        for r in root.xpath('//div[@class="box"]/table/tbody/tr'):
            if len(r.xpath('td[@align="center"]')) > 0:
                if len(r.xpath('./td[2]/a')) > 0:
                    zhang = r.xpath('./td[2]/a/text()')[0]
                else:
                    jie = "".join(r.xpath('./td[2]//text()')).replace("\r", "").replace("\n", "").strip()
                    tmp = r.xpath('./td[5]/a/@href')[0]
                    tmp = tmp.split('-')
                    tiNo = tmp[2].split(".")[0]
                    CourseID = tmp[1]
                    # 获取小节试题
                    self.get_lx(kemu, zhang, jie, CourseID, tiNo)

    # 仿真考场
    def get_fz(self, kemu):
        url = "http://qqks.net/simulation-9-2.html"
        res = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(res.text)
        for r in root.xpath('//div[@class="box"]/table/tbody/tr'):
            if len(r.xpath('td[@align="center"]')) > 0:
                if len(r.xpath('./td[2]/a')) > 0:
                    zhang = r.xpath('./td[2]/a/text()')[0]
                    data_SubjectId = r.xpath('./td[3]/a/@data-subjectid')[0]
                    data_id = r.xpath('./td[3]/a/@data-id')[0]
                    # 获取小节试题
                    id = self.get_id("2", data_SubjectId, data_id)
                    # 获取具体题号
                    self.get_ti_1(kemu, "仿真考场", zhang, id)

    # 随机测试
    def get_sj(self, kemu):
        url = "http://qqks.net/randomexam-9-1.html"
        res = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(res.text)
        for r in root.xpath('//div[@class="box"]/table/tbody/tr'):
            if len(r.xpath('td[@align="center"]')) > 0:
                if len(r.xpath('./td[2]/a')) > 0:
                    zhang = r.xpath('./td[2]/a/text()')[0]
                    data_SubjectId = r.xpath('./td[3]/a/@data-subjectid')[0]
                    data_id = r.xpath('./td[3]/a/@data-id')[0]
                    # 获取小节试题
                    id = self.get_id("1", data_SubjectId, data_id)
                    # 获取具体题号
                    self.get_ti_1(kemu, "随机测试", zhang, id)

    # 考前押题
    def get_yt(self, kemu):
        url = "http://qqks.net/testbefore-8-1.html"
        res = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(res.text)
        for r in root.xpath('//div[@class="box"]/table/tbody/tr'):
            if len(r.xpath('td[@align="center"]')) > 0:
                if len(r.xpath('./td[2]/a')) > 0:
                    zhang = r.xpath('./td[2]/a/text()')[0]
                    data_SubjectId = r.xpath('./td[3]/a/@data-subjectid')[0]
                    data_id = r.xpath('./td[3]/a/@data-id')[0]
                    # 获取小节试题
                    id = self.get_id("3", data_SubjectId, data_id)
                    # 获取具体题号
                    self.get_ti_1(kemu, "考前押题", zhang, id)


    # 练习-获取“节”的题号
    def get_lx(self, kemu ,zhang, jie, CourseID, id):
        tableName = zhang
        url = f"http://qqks.net/Ajax/CoreAjax.ashx"
        postData = {
            'Type': 'GetLianxiIdYh',
            'CourseID': CourseID,
            'SectionId': id,
            'TypeId': '1,2,3,6',
            'IsKeyNote': 'false'
        }
        html = HttpUtils.do_request("POST", url, self.headers, postData)

        data_json = json.loads(html.text)
        tiNo_list = data_json['shitiid']  # 题号

        # 获取每题内容及答案
        self.get_da_1(kemu, zhang, jie, tiNo_list)

    # 获取题目内容。根据节
    def get_da_1(self, kemu, zhang, jie, tiNo_list):
        tableName = kemu
        url = f"http://qqks.net/Ajax/CoreAjax.ashx"
        for index, value in enumerate(tiNo_list):
            postData = {
                'Type': 'GetLianxiYh',
                'SubjectId': value,
                'Number': index

            }

            html = HttpUtils.do_request("POST", url, self.headers, postData)
            data_json = json.loads(html.text)

            if data_json['type'] in ("单选题", "多选题", "判断题"):
                dict_data = dict()
                dict_data['科目'] = kemu
                dict_data['类型'] = "章节练习"
                dict_data['章'] = zhang
                dict_data['节'] = jie
                dict_data['题型'] = data_json['type']  # 题型
                dict_data['标题'] = data_json['title']  # 标题
                tag_count = 1
                for t in data_json['options']:
                    dict_data['选项_' + str(tag_count)] = t.replace("&nbsp;", "")
                    tag_count += 1
                dict_data['答案'] = data_json['answer']  # 答案
                dict_data['解析'] = data_json['demo']  # 解析

                self.insertItem(tableName, dict_data)
                print(str(dict_data))
            elif data_json['type'] == "不定项选择题":
                answer = data_json['answer'].split(";;")
                demo = data_json['demo'].split(";;")
                for index, value in enumerate(data_json['options']):
                    dict_data = dict()
                    dict_data['科目'] = kemu
                    dict_data['类型'] = "章节练习"
                    dict_data['章'] = zhang
                    dict_data['节'] = jie
                    dict_data['题型'] = data_json['type']  # 题型
                    dict_data['标题'] = data_json['title']  # 标题
                    tag_count = 1
                    dict_data['子问题'] = value['title']
                    for o in value['options']:
                        dict_data['选项_' + str(tag_count)] = o.replace("&nbsp;", "")
                        tag_count += 1
                    dict_data['答案'] = answer[index]  # 答案
                    dict_data['解析'] = demo[index]  # 解析

                    self.insertItem(tableName, dict_data)
                    print(str(dict_data))
            else:
                aa = data_json['type']  # 题型


    # 考前押题typeid=3仿真考场typeid=2随机测试typeid=1 - 获取题号
    def get_id(self, typeid, subjectId, paperid):
        url = "http://qqks.net/Ajax/CoreAjax.ashx"
        postData = {
            'Type': 'SetInitialize',
            'subjectId': subjectId,
            'paperid': paperid,
            'typeid': typeid,
            'round': 196,
        }
        res = HttpUtils.do_request("POST", url, self.headers, postData)
        data_json = json.loads(res.text)
        id = data_json['reid']

        return id

    def get_ti_1(self, kemu,listtype, zhang, examid):
        url = "http://qqks.net/Ajax/CoreAjax.ashx"
        postData  = {
            'Type': 'GetSubjectChuji',
            'examid': examid,
            'mode':''
        }
        res = HttpUtils.do_request("POST", url, self.headers, postData)
        root = etree.HTML(res.text)
        for r in root.xpath('//div[@class="parenttrclass"]'):
            thid = r.xpath('.//div[@class="Exam_ans"]/input[@id="txtExamSubjectIDs"]/@value')[0]
            title = r.xpath('.//div[@class="Exam_wen"]/span/text()')[0]
            if len(r.xpath('.//div[@class="left_top"]/strong/text()')) > 0:
                typename = r.xpath('.//div[@class="left_top"]/strong/text()')[0].split("、")[1]
                typename = re.findall(r'^(.*?)\(', typename)[0].strip()
            self.get_da(kemu, listtype, zhang, typename, examid, thid, title)

    # 获取答案
    def get_da(self, kemu, listtype, zhang, typename, djid, thid, title):
        url = "http://qqks.net/Ajax/CoreAjax.ashx"
        postData  = {
            'Type': 'GetAnalytical',
            'djid': djid,
            'thid': thid,
            'title': title
        }
        res = HttpUtils.do_request("POST", url, self.headers, postData)
        root = etree.HTML(res.text)
        if typename in ("单选题", "多选题", "判断题"):
            dict_data = dict()
            dict_data['科目'] = kemu
            dict_data['类型'] = listtype
            dict_data['章'] = zhang
            dict_data['节'] = ""
            dict_data['题型'] = typename  # 题型
            dict_data['标题'] = "".join(root.xpath('//div[@class="topic"]//text()')).split("、")[1].replace("\r", "").replace("\n", "").replace("\t", "").replace("1、", "") # 标题
            tag_count = 1
            for t in root.xpath('//div[@class="options"]/ul/li'):
                dict_data['选项_' + str(tag_count)] = t.xpath('./text()')[0].replace("&nbsp;", "")
                tag_count += 1
            dict_data['答案'] = root.xpath('//div[@class="trow"][2]/div[3]/text()')[0]  # 答案
            dict_data['解析'] = "".join(root.xpath('//p//text()'))  # 解析

            self.insertItem(kemu, dict_data)
            print(str(dict_data))
        elif typename == "不定项选择题":
            line = 1

            for t in root.xpath('//div[@class="topic"]'):
                dict_data = dict()
                dict_data['科目'] = kemu
                dict_data['类型'] = listtype
                dict_data['章'] = zhang
                dict_data['节'] = ""
                dict_data['题型'] = "不定项选择题"
                dict_data['标题'] = "".join(root.xpath('//div[@class="topic"][1]//text()')).replace("[", "").replace("]", "")

                if line == 1:
                    pass
                else:
                    tag_count = 1
                    # dict_data['子问题'] = t.xpath('./text()')[0]
                    # for o in t.xpath('./br'):
                    #     dict_data['选项_' + str(tag_count)] = o.tail.replace("&nbsp;", "")
                    #     tag_count += 1
                    #     flag = f'//div[@class="detailed"][{line - 1}]//div[@class="trow"][2]/div[3]/text()'
                    #     flag1 = f'//div[@class="detailed"][{line - 1}]//p/text()'
                    #     dict_data['答案'] = root.xpath(flag)[0]  # 答案
                    #     dict_data['解析'] = "".join(root.xpath(flag1)).replace("\r", "").replace("\n", "")  # 解析
                    # tmp = "".join(t.xpath('./text()')).split(") .")
                    # tmp = re.findall(r'(.*?)\([A-Z]\)', t.xpath('.//text()'))[0].strip()
                    # dict_data['子问题'] = tmp[0].replace("\n", "").strip().replace("(A", "").replace("(B", "").replace("(C", "").replace("(D", "")
                    # tmp = "".join(t.xpath('./text()')).split("(A)")
                    # dict_data['子问题'] = tmp[0].replace("\n", "").strip().replace("(A", "").replace("(B", "").replace("(C", "").replace("(D", "")
                    # key_list = ["(B)", "(C)", "(D)"]
                    # for index, value in enumerate(tmp):
                    #     if index > 0:
                    #         dict_data['选项_' + str(tag_count)] = value.replace("(A", "").replace("(B", "").replace("(C", "").replace("(D", "")
                    #         tag_count += 1
                    #         flag = f'//div[@class="detailed"][{line - 1}]//div[@class="trow"][2]/div[3]/text()'
                    #         flag1 = f'//div[@class="detailed"][{line - 1}]//p/text()'
                    #         dict_data['答案'] = root.xpath(flag)[0]  # 答案
                    #         dict_data['解析'] = "".join(root.xpath(flag1)).replace("\r", "").replace("\n", "")  # 解析
                    tmp = "".join(t.xpath('.//text()')).split("(A)")
                    dict_data['子问题'] = tmp[0].replace("\n", "").strip().replace("(A", "").replace("(B", "").replace("(C", "").replace("(D", "")
                    dict_data['选项_1'] = tmp[1].split("(B)")[0]
                    # dict_data['选项_2'] = tmp[1].split("(B)")[1].split("(C)").split("(c)")[0]
                    if "c" in tmp[1].split("(B)")[1]:
                        dict_data['选项_2'] = tmp[1].split("(B)")[1].split("(c)")[0]
                        dict_data['选项_3'] = tmp[1].split("(c)")[1].split("(D)")[0]
                    if "C" in tmp[1].split("(B)")[1]:
                        dict_data['选项_2'] = tmp[1].split("(B)")[1].split("(C)")[0]
                        dict_data['选项_3'] = tmp[1].split("(C)")[1].split("(D)")[0]
                    # dict_data['选项_3'] = tmp[1].split("(C)")[1].split("(D)")[0]
                    dict_data['选项_4'] = tmp[1].split("(D)")[1]
                    flag = f'//div[@class="detailed"][{line - 1}]//div[@class="trow"][2]/div[3]/text()'
                    flag1 = f'//div[@class="detailed"][{line - 1}]//p//text()'
                    flag2 = f'//div[@class="detailed"][{line - 1}]//span//text()'
                    dict_data['答案'] = root.xpath(flag)[0]  # 答案
                    jixi = "".join(root.xpath(flag1)).replace("\r", "").replace("\n", "")  # 解析
                    jixi += "".join(root.xpath(flag2))
                    dict_data['解析'] = jixi  # 解析
                line += 1
                self.insertItem(kemu, dict_data)
                print(str(dict_data))
        else:
            print(typename)


    def insertItem(self, tableName, data):
        my_set = db[tableName]
        my_set.insert_one(data)


if __name__ == '__main__':
    conn = MongoClient('127.0.0.1', 27017)
    db = conn["Test"]

    test_get = Test_Get()

    # # 章节练习 初级会计实务  初级经济法基础
    # test_get.get_mulu("初级经济法基础")

    # 仿真考场  注意翻页
    # test_get.get_fz("初级经济法基础")

    # 随机测试
    # test_get.get_sj("初级经济法基础")

    # 考前押题 修改 get_da 方法中，提取tmp /text()
    test_get.get_yt("初级经济法基础2")

